<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateLegalFormTable extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('legal_form', function (Blueprint $table) {
            $table->id()->from(config('migration.from'));

            $table->bigInteger('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');

            $table->bigInteger('personal_id')->unsigned()->comment('Персональные данные');
            $table->foreign('personal_id')->references('id')->on('personal_data')->onDelete('restrict')->onUpdate('restrict');

            $table->bigInteger('passport_id')->unsigned()->comment('Паспортные данные');
            $table->foreign('passport_id')->references('id')->on('passport_data')->onDelete('restrict')->onUpdate('restrict');

            $table->bigInteger('company_id')->unsigned()->nullable()->comment('Данные компании');
            $table->foreign('company_id')->references('id')->on('company_data')->onDelete('restrict')->onUpdate('restrict');

            $table->jsonb('documents')->default(json_encode([]));
            $table->index('documents', null, 'gin');

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('legal_form');
    }
}
